﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.ViewModels.DialogViewModels/EditeUserDialogModel
* 类       名: EditeUserDialogModel
* 功      能:  TD
* 时      间:  2024-05-24 11:24:27
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WaferSelectV3.Common;
using WaferSelectV3.DAL.DataEntity;
using WaferSelectV3.DAL.LocalDataAccess;
using WaferSelectV3.Models;

namespace WaferSelectV3.ViewModels.DialogViewModels
{
    public class EditeUserDialogModel : BindableBase, IDialogAware
    {
        public DelegateCommand ModifyCommand { get; private set; }
        public DelegateCommand CancelCommand { get; private set; }

        public UserEntity? NewUser;

        public EditeUserDialogModel()
        {
            ModifyCommand = new DelegateCommand(DoModify);
            CancelCommand = new DelegateCommand(DoCancel);
        }

        private void DoCancel()
        {
            RequestClose?.Invoke(new DialogResult());
        }

        private void DoModify()
        {
            if (string.IsNullOrWhiteSpace(Username) || string.IsNullOrWhiteSpace(Password))
            {
                MessageBox.Show("用户名和密码不能为空", "用户名和密码输入错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            NewUser!.Role = (int)Enum.Parse(typeof(RoleType), RoleList![SelectedIndex]);
            NewUser.UserName = Username;
            NewUser.Password = Password;
            DataResult result = UserAccess.UpdateDataSingle(NewUser);
            if (!result.State)
            {
                MessageBox.Show(result.Message!, "数据库操作错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            RequestClose?.Invoke(new DialogResult());
        }

        private ObservableCollection<string>? _roleList;

        public ObservableCollection<string>? RoleList
        {
            get { return _roleList; }
            set
            {
                _roleList = value;
                RaisePropertyChanged();
            }
        }

        private string? _username;

        public string? Username
        {
            get { return _username; }
            set
            {
                _username = value;
                RaisePropertyChanged();
            }
        }

        private string? _password;

        public string? Password
        {
            get { return _password; }
            set
            {
                _password = value;
                RaisePropertyChanged();
            }
        }

        private int _selectedIndex;

        public int SelectedIndex
        {
            get { return _selectedIndex; }
            set
            {
                _selectedIndex = value;
                RaisePropertyChanged();
            }
        }


        private string? _title;

        public string? Title
        {
            get { return _title; }
            set
            {
                _title = value;
                RaisePropertyChanged();
            }
        }


        public event Action<IDialogResult>? RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            Username = null;
            Password = null;
            Title = null;
            NewUser = null;
            RoleList?.Clear();
            RoleList = null;
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            if (parameters != null)
            {
                parameters.TryGetValue("Title", out string titleValue);
                parameters.TryGetValue("UserInfo", out UserModel userModel);
                if (!string.IsNullOrWhiteSpace(titleValue) && userModel != null)
                {
                    // 创建默认要显示的用户信息
                    RoleList = new ObservableCollection<string>()
                    {
                        Enum.GetName(typeof(RoleType),RoleType.操作员)!,
                        Enum.GetName(typeof(RoleType),RoleType.管理员)!,
                        Enum.GetName(typeof(RoleType),RoleType.工程师)!
                    };
                    Username = userModel.Username;
                    Password = userModel.Password;
                    Title = titleValue;
                    SelectedIndex = RoleList.IndexOf(Enum.GetName(typeof(RoleType), userModel.Role)!);

                    NewUser = new UserEntity()
                    {
                        Id = userModel.Id,
                        UserName = Username,
                        Password = Password,
                        CreateBy = userModel.CreateBy,
                        CreateTime = userModel.CreateTime,
                        LastLoginTime = userModel.LastLoginTime,
                    };
                }
            }
        }
    }
}
